//
//  UIViewController+SCNavBarItem.m
//  Navigation
//
//  Created by dangshuai on 2019/2/22.
//  Copyright © 2019 dangshuai. All rights reserved.
//

#import "UIViewController+SCNavBarItem.h"

@implementation UIViewController (SCNavBarItem)
- (SCBarButtonItemView *)rightBarItemWithTitle:(NSString *)title action:(SEL)act {
    SCBarButtonItemView *right = [SCBarButtonItemView buttonWithType:UIButtonTypeCustom];
    right.titleLabel.font = right.titleFont;
//    right.backgroundColor = [UIColor lightGrayColor];
    [right setTitle:title forState:0];
    [right setTitleColor:SC_TextBlack_Primary forState:0];
    [right addTarget:self action:act forControlEvents:UIControlEventTouchUpInside];
    CGRect rect = [self titleRect:title];
    UIBarButtonItem *rightBar = [[UIBarButtonItem alloc] initWithCustomView:right];
    if (@available(iOS 11.0, *)) {
        right.alignmentRectInsetsOverride = UIEdgeInsetsMake(0, 0, 0, 16);
        right.translatesAutoresizingMaskIntoConstraints = NO;
        [right.widthAnchor constraintEqualToConstant:CGRectGetWidth(rect) - 16].active = YES;
        [right.heightAnchor constraintEqualToConstant:44].active = YES;
    } else {
        right.frame = rect;
    }
    
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spacer.width = -16;
    self.navigationItem.rightBarButtonItems = @[spacer,rightBar];
    return right;
}

- (SCBarButtonItemView *)rightBarItemWithImage:(NSString *)image action:(SEL)act {
    SCBarButtonItemView *right = [SCBarButtonItemView buttonWithType:UIButtonTypeCustom];
//    right.backgroundColor = [UIColor lightGrayColor];
    [right setImage:[UIImage imageNamed:image] forState:0];
    
    [right addTarget:self action:act forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBar = [[UIBarButtonItem alloc] initWithCustomView:right];
    if (@available(iOS 11.0, *)) {
        right.alignmentRectInsetsOverride = UIEdgeInsetsMake(0, 0, 0, 16);
        right.translatesAutoresizingMaskIntoConstraints = NO;
        [right.widthAnchor constraintEqualToConstant:24].active = YES;
        [right.heightAnchor constraintEqualToConstant:44].active = YES;
    } else {
        right.alignmentRectInsetsOverride = UIEdgeInsetsMake(0, -10, 0, 10);
        right.frame = CGRectMake(0, 0, 40, 44);
    }
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spacer.width = -10;
    self.navigationItem.rightBarButtonItems = @[spacer,rightBar];
    return right;
}

- (void)rightBarItemWithCustomView:(SCBarButtonItemView *)right {
    [self rightBarItemWithCustomView:right rightSpace:0];
}

- (void)rightBarItemWithCustomView:(SCBarButtonItemView *)right rightSpace:(CGFloat)space {
    UIBarButtonItem *rightBar = [[UIBarButtonItem alloc] initWithCustomView:right];
    if (@available(iOS 11.0, *)) {
        right.alignmentRectInsetsOverride = UIEdgeInsetsMake(0, 0, 0, 16-space);
        right.translatesAutoresizingMaskIntoConstraints = NO;
        [right.widthAnchor constraintEqualToConstant:CGRectGetWidth(right.frame)].active = YES;
        [right.heightAnchor constraintEqualToConstant:CGRectGetHeight(right.frame)].active = YES;
    } else {
        right.alignmentRectInsetsOverride = UIEdgeInsetsMake(0, -10, 0, 10-space);
    }
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spacer.width = -10;
    self.navigationItem.rightBarButtonItems = @[spacer,rightBar];
}

- (void)rightBarItemsWithCustomArray:(NSMutableArray *)buttons {
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spacer.width = -10;
    [buttons addObject:spacer];
    
    
    NSMutableArray *items = [NSMutableArray arrayWithCapacity:0];
    if (@available(iOS 11.0, *)) {
        for (SCBarButtonItemView *button in buttons) {
            if ([button isKindOfClass:[UIBarButtonItem class]]) {
                continue;
            }
            button.alignmentRectInsetsOverride = UIEdgeInsetsMake(0, 0, 0, 7);
            button.translatesAutoresizingMaskIntoConstraints = NO;
            [button.widthAnchor constraintEqualToConstant:CGRectGetWidth(button.frame)].active = YES;
            [button.heightAnchor constraintEqualToConstant:CGRectGetHeight(button.frame)].active = YES;
            UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:button];
            [items addObject:barItem];
        }
    } else {
        for (SCBarButtonItemView *button in buttons) {
            if ([button isKindOfClass:[UIBarButtonItem class]]) {
                continue;
            }
            button.alignmentRectInsetsOverride = UIEdgeInsetsMake(0, 0, 0, 7);
            UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:button];
            [items addObject:barItem];
        }
    }
    self.navigationItem.rightBarButtonItems = items;
}

- (void)rightBarItemWithCustomViews:(SCBarButtonItemView *)buttons,... {
    
    NSMutableArray *marray = [NSMutableArray arrayWithCapacity:0];
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spacer.width = -10;
    [marray addObject:spacer];
    
    
    if (@available(iOS 11.0, *)) {
        buttons.alignmentRectInsetsOverride = UIEdgeInsetsMake(0, 0, 0, 6);
        buttons.translatesAutoresizingMaskIntoConstraints = NO;
        [buttons.widthAnchor constraintEqualToConstant:CGRectGetWidth(buttons.frame)].active = YES;
        [buttons.heightAnchor constraintEqualToConstant:CGRectGetHeight(buttons.frame)].active = YES;
    }
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:buttons];
    [marray addObject:barItem];
    
    va_list args;
    va_start(args, buttons);
    if (buttons) {
        SCBarButtonItemView *button;
        if (@available(iOS 11.0, *)) {
            while ((button = va_arg(args, SCBarButtonItemView *))) {
                SCBarButtonItemView *btn = (SCBarButtonItemView *)button;
                btn.alignmentRectInsetsOverride = UIEdgeInsetsMake(0, 0, 0, 7);
                btn.translatesAutoresizingMaskIntoConstraints = NO;
                [btn.widthAnchor constraintEqualToConstant:CGRectGetWidth(btn.frame)].active = YES;
                [btn.heightAnchor constraintEqualToConstant:CGRectGetHeight(btn.frame)].active = YES;
                UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
                [marray addObject:barItem];
            }
            self.navigationItem.rightBarButtonItems = marray;
        } else {
            while ((button = va_arg(args, SCBarButtonItemView *))) {
                SCBarButtonItemView *btn = (SCBarButtonItemView *)button;
                btn.alignmentRectInsetsOverride = UIEdgeInsetsMake(0, 0, 0, 7);
                UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
                [marray addObject:barItem];
            }
            self.navigationItem.rightBarButtonItems = marray;
        }
    }
    va_end(args);
}

#pragma mark -- Left

- (SCBarButtonItemView *)backItemAndOtherItemImage:(NSString *)image action:(SEL)act {
    
    SCBarButtonItemView *back = [SCBarButtonItemView buttonWithType:UIButtonTypeCustom];
    [back setImage:[UIImage imageNamed:@"back_icon"] forState:0];
    [back addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [back setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:back];
    
    SCBarButtonItemView *left = [SCBarButtonItemView buttonWithType:UIButtonTypeCustom];
    [left setImage:[UIImage imageNamed:image] forState:0];
    [left addTarget:self action:act forControlEvents:UIControlEventTouchUpInside];
    [left setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:left];
    
    if (@available(iOS 11.0, *)) {
        back.alignmentRectInsetsOverride = UIEdgeInsetsMake(-3, 8, 0, 0);
        back.translatesAutoresizingMaskIntoConstraints = NO;
        [back.widthAnchor constraintEqualToConstant:27].active = YES;
        [back.heightAnchor constraintEqualToConstant:44].active = YES;
        
        left.alignmentRectInsetsOverride = UIEdgeInsetsMake(-2, 8, 0, 0);
        left.translatesAutoresizingMaskIntoConstraints = NO;
        [left.widthAnchor constraintEqualToConstant:27].active = YES;
        [left.heightAnchor constraintEqualToConstant:44].active = YES;
        
        UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        spacer.width = -8;
        self.navigationItem.leftBarButtonItems = @[spacer, backItem, leftItem];
    } else {
        back.alignmentRectInsetsOverride = UIEdgeInsetsMake(-4, 8, 0, 0);
        left.alignmentRectInsetsOverride = UIEdgeInsetsMake(-4, 8, 0, 0);
        back.frame = CGRectMake(0, 0, 35, 44);
        left.frame = CGRectMake(0, 0, 35, 44);
        self.navigationItem.leftBarButtonItems = @[ backItem, leftItem];
    }
    return left;
}

- (SCBarButtonItemView *)leftItemWithImage:(NSString *)image action:(SEL)act {
    SCBarButtonItemView *left = [SCBarButtonItemView buttonWithType:UIButtonTypeCustom];
    [left setImage:[UIImage imageNamed:image] forState:0];
    [left addTarget:self action:act forControlEvents:UIControlEventTouchUpInside];
    [left setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:left];
    if (@available(iOS 11.0, *)) {
        left.alignmentRectInsetsOverride = UIEdgeInsetsMake(-2, 8, 0, 0);
        left.translatesAutoresizingMaskIntoConstraints = NO;
        [left.widthAnchor constraintEqualToConstant:27].active = YES;
        [left.heightAnchor constraintEqualToConstant:44].active = YES;
        
        UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        spacer.width = -8;
        self.navigationItem.leftBarButtonItems = @[spacer, leftItem];
    } else {
        left.alignmentRectInsetsOverride = UIEdgeInsetsMake(-4, 8, 0, 0);
        left.frame = CGRectMake(0, 0, 35, 44);
        self.navigationItem.leftBarButtonItem = leftItem;
    }
    return left;
}

- (CGRect)titleRect:(NSString *)title {
    CGRect rect = [title boundingRectWithSize:CGSizeMake(200, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:NULL];
    rect.size.width += 18;
    rect.size.height = 44;
    return rect;
}

- (void)backButtonClick{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
